package com.car.order.controller;

import cn.hutool.core.bean.BeanUtil;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.StateUtil;
import com.car.commons.web.util.WebUtil;
import com.car.order.annotation.StockLogAnnotation;
import com.car.order.dto.stock.StockDto;
import com.car.order.entity.Stock;
import com.car.order.entity.form.stock.*;
import com.car.order.entity.param.stock.StockInsertParam;
import com.car.order.entity.param.stock.StockUpdateParam;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;

import org.springframework.web.bind.annotation.*;
import com.car.order.service.StockService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;

/**
 *
 *
 * @author 杜岷芫
 * @since 2024-08-29 11:43:37
 */
@RestController
@RequestMapping("/stock")
@Api(tags = "门店仓库信息")
public class StockController {

    @Autowired
    private StockService stockService;

    /**
     * 分页查询
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("/findPage.md")
    public Result findPage(@RequestBody StockFindPageForm form) {
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        form.setClerkId(clerkId);
        Page<StockDto> result = stockService.findPage(form);
        return Result.getResult().setMsg("查询成功").setData(result);
    }
    /**
     * 列表查询
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "列表查询")
    @PostMapping("/findList")
    public Result findList(@RequestBody StockFindListForm form) {
        List<Stock> result = stockService.findList(form);
        return Result.getResult().setMsg("查询成功").setData(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询详情")
    @GetMapping("/{id}.md")
    public Result findById(@PathVariable("id") Integer id) {
        Stock stock = stockService.findById(id);
        return Result.getResult().setMsg("查询成功").setData(stock);
    }

    /**
     * 新增
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "平台员工新增，feign接口")
    @PostMapping("/insert.ty")
    public Result insert( @RequestBody StockInsertForm form) {
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer adminId = JwtUtil.getAdminId(token);
        StockInsertParam param = BeanUtil.toBean(form, StockInsertParam.class);
        Stock stock = stockService.insert(param);
        return Result.getResult().setMsg("操作成功").setData(stock);
    }

    @ApiOperation(value = "门店员工新增")
    @PostMapping("/clerkInsert")
    public Result clerkInsert( @RequestBody StockClerkInsertForm form) {
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        StockInsertParam param = BeanUtil.toBean(form, StockInsertParam.class);
        param.setClerkId(clerkId);
        Stock stock = stockService.clerkIdInsert(param);
        return Result.getResult().setMsg("操作成功").setData(stock);
    }

    /**
     * 修改
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "增加库存量")
    @PutMapping("/add")
    public Result add( @RequestBody StockUpdateForm form) {
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        StockUpdateParam param = BeanUtil.toBean(form, StockUpdateParam.class);
        param.setAdminId(clerkId);
        Stock stock = stockService.add(param);
        return Result.getResult().setMsg("操作成功").setData(stock);
    }

    @ApiOperation(value = "减少库存")
    @PutMapping("/reduce")
    public Result reduce( @RequestBody StockUpdateForm form) {
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        StockUpdateParam param = BeanUtil.toBean(form, StockUpdateParam.class);
        param.setAdminId(clerkId);
        Stock stock = stockService.reduce(param);
        return Result.getResult().setMsg("操作成功").setData(stock);
    }

    @ApiOperation(value = "修改库存")
    @PutMapping("/changeStock.md")
    public Result changeStock(@RequestBody StockUpdateForm form) {
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        StockUpdateParam param = BeanUtil.toBean(form, StockUpdateParam.class);
        param.setAdminId(clerkId);
        Stock stock = stockService.changeStock(param);
        return Result.getResult().setMsg("操作成功").setData(stock);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "修改状态")
    @PutMapping("/changeState/{id}/{state}")
    public Result delete(@PathVariable("id") Integer id,@PathVariable("state") Integer state) {
        stockService.changeState(id,state);
        return Result.getResult().setMsg("操作成功");
    }

}